package com.zhu.aliyunService;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import lombok.Data;

import lombok.experimental.Accessors;
import org.junit.Test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.List;

@SpringBootTest
public class VODClient {

    private String accessKeyId="LTAI5tCx5go23Q2UABmdHCyy";
    private String accessKeySecret="4jQxvx6vp2FDu3tZP5s9Hfk120BXjZ";

    /**
     * 创建服务
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     */
    /**
     * 创建服务
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     */
    public static DefaultAcsClient initCilent(String accessKeyId, String accessKeySecret){
        String regionId="cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient acsClient=new DefaultAcsClient(profile);
        return  acsClient;

    }

    /**
     * 获取播放地址函数
     * @param client
     * @return
     * @throws ClientException
     */
    public static GetPlayInfoResponse getPlayInfoResponse(DefaultAcsClient client) throws ClientException {
        GetPlayInfoRequest getPlayInfoRequest=new GetPlayInfoRequest();
//        getPlayInfoRequest.setEndpoint("oss-cn-shanghai.aliyuncs.com");
        getPlayInfoRequest.setVideoId("\n" +
                "71b0401da405486cb95235f26173924a");
        return client.getAcsResponse(getPlayInfoRequest);
    }
    @Test
    public void test() throws Exception {
        VODClient vodClient=new VODClient();
        DefaultAcsClient acsClient = vodClient.initCilent(vodClient.accessKeyId, vodClient.accessKeySecret);
        GetPlayInfoResponse response = VODClient.getPlayInfoResponse(acsClient);
        System.out.println(response.getRequestId());
        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.println("playInfoUrl="+playInfo.getPlayURL()+"\n");
        }
    }

    /**
     * 获取视频播放凭证
     */

    public static GetVideoPlayAuthResponse getVideoPlayAuthResponse(DefaultAcsClient client) throws ClientException {
        GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
        request.setVideoId("41b6955d9dd84519a1b1de07034f562b");

        return client.getAcsResponse(request);
    }

    @Test
    public  void testVideoPlayAuth(){
        DefaultAcsClient client=initCilent(accessKeyId,accessKeySecret);
        GetVideoPlayAuthResponse response=new GetVideoPlayAuthResponse();
        try {
            response=getVideoPlayAuthResponse(client);
            System.out.println("auth:"+response.getPlayAuth());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
